package com.lfy.modules.monitor.controller;


import com.lfy.modules.common.anno.Authentication;
import com.lfy.modules.common.anno.NoNeedLogin;
import com.lfy.modules.common.domain.PageResultVO;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.monitor.domain.vo.SystemDetailVO;
import com.lfy.modules.monitor.domain.vo.ThreadInformationVO;
import com.lfy.modules.monitor.domain.vo.ThreadPoolDetailVO;
import com.lfy.modules.monitor.domain.vo.ThreadPoolMonitorVO;
import com.lfy.modules.monitor.service.MonitorService;
import com.lfy.modules.monitor.service.ThreadPoolDetailService;
import com.lfy.modules.monitor.task.SseTaskCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lfy
 * @since 2024-04-02
 */
@RestController
@RequestMapping("/admin/Monitor")
@Slf4j
@Validated
public class ThreadPoolMonitorController {

    @Resource
    private MonitorService monitorService;
    @Resource
    private ThreadPoolDetailService threadPoolDetailService;

    /**
     * 修改线程池最大线程数
     *
     * @param maxSize
     * @return
     */
    @PostMapping("/update/maxSize")
    @Authentication("sys:monitor:update")
    public R<Void> updateThreadPoolMaxSize(@Validated @NotNull(message = "最大线程数不能为空") Integer maxSize) {
        threadPoolDetailService.updateThreadPoolMaxSize(maxSize);

        return R.success();
    }

    /**
     * 修改核心线程数
     *
     * @param coreSize
     * @return
     */
    @PostMapping("/update/coreSize")
    @Authentication("sys:monitor:update")
    public R<Void> updateThreadPoolCoreSize(@Validated @NotNull(message = "核心线程数不能为空") Integer coreSize) {
        threadPoolDetailService.updateThreadPoolCoreSize(coreSize);

        return R.success();
    }

    /**
     * 修改队列大小
     *
     * @param queueSize
     * @return
     */
    @PostMapping("/update/queueSize")
    @Authentication("sys:monitor:update")
    public R<Void> updateThreadPoolQueueSize(@Validated @NotNull(message = "队列大小不能为空") Integer queueSize) {

        threadPoolDetailService.updateThreadPoolQueueSize(queueSize);

        return R.success();
    }

    /**
     * 开/关告警
     */
    @PostMapping("/update/openAlarm")
    @Authentication("sys:monitor:update")
    public R<Void> updateThreadAlarm(@Validated @NotNull(message = "不能为空")
                                     @Max(value = 1, message = "开关只能为0或1")
                                     @Min(value = 0, message = "开关只能为0或1") Integer openAlarm) {
        threadPoolDetailService.updateThreadAlarm(openAlarm);
        return R.success();
    }
    /**
     * 获取当前线程池信息
     */
    @GetMapping("/get/information")
    @Authentication("sys:monitor:get")
    public R<ThreadInformationVO> getThreadInformation(){
        return threadPoolDetailService.getInformation();
    }
    /**
     * 获取线程池分析数据
     *
     * @return r
     */
    @GetMapping("/get/mainThreadPool")
    @Authentication("sys:monitor:get")
    public R<ThreadPoolDetailVO> getThreadPoolDetail() {
        return monitorService.getThreadPoolDetail();
    }

    /**
     * 分页获取线程池数据
     *
     * @return r
     */
    @GetMapping("/get/threadList")
    @Authentication("sys:monitor:get")
    public R<PageResultVO<ThreadPoolMonitorVO>> getThreadList(@RequestParam("pageSize") Integer pageSize,
                                                              @RequestParam("pageNum") Integer pageNum) {
        return monitorService.getThreadList(pageSize, pageNum);
    }

    /**
     * 获取操作系统信息
     */
    @GetMapping("/get/systemDetail")
    @Authentication("sys:monitor:get")
    public R<SystemDetailVO> getSystemDetail() {
        return monitorService.getSystem();
    }

    /**
     *
     * @return
     */
    @GetMapping("/SseChannel")
    @NoNeedLogin
    public SseEmitter createChannel(HttpServletRequest request) {
        String referer = request.getHeader("referer");
        //TODO 这里应该判断referer是否合法，防止恶意请求
        log.info(referer);
        return SseTaskCache.createChannel();
    }

}

